// pages/order/index.js
import config from '../../etc/config'
const {
  $Toast
} = require('../../components/dist/base/index');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList: [],
    current: '1',
    currentPage: 1,
    commcate: '1',
    content: '',
    hiddenhandlerComment: true, // 隐藏或者显示弹框
    hidden: false,
    attitudeRate: 5,
    speedRate: 5,
    orderId: '',
    buyerremake: '',
    buyerRemake: '',
    tab: [{
        name: '好评',
        img: '/images/order/laugh.png',
        ids: '1'
      },
      {
        name: '中评',
        img: '/images/order/smile.png ',
        ids: '2'
      },
      {
        name: '差评',
        img: '/images/order/unhappy.png',
        ids: '3'
      }
    ],
    ids: 1,
    click: true
  },
  click(e) {
    var ids = e.currentTarget.dataset.ids;
    this.setData({
      ids: ids
    })

  },
  // 查看订单详情
  enterContentDetail(e) {
    wx.navigateTo({
      url: '../order-detail/index?orderId=' + e.currentTarget.dataset.orderid,
    })
  },
  tagClick() {
    this.data.attitudeRate = ''
    this.data.speedRate = ''
    this.data.content = ''
    this.setData({
      hidden: true
    })
  },

  handlerComment(e) {
    this.setData({
      hiddenhandlerComment: !this.data.hiddenhandlerComment,
      orderId: e.currentTarget.dataset.orderid,
      buyerremake: e.currentTarget.dataset.buyerremake
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    wx.hideShareMenu()
  },
  cancel: function() {
    this.setData({
      hidden: false,
    })
  },

  // 态度
  handlerAttitudeRate(e) {
    const index = e.detail.index;
    this.setData({
      'attitudeRate': index
    })
  },
  //速度
  handlerSpeedRate(e) {
    const index = e.detail.index;
    this.setData({
      'speedRate': index
    })
  },
  valueInput(e) {
    this.setData({
      content: e.detail.value
    })
  },
  //确定
  handlerPublishComment() {
    var that = this
    let params = {
      orderId: this.data.orderId,
      attitudeScore: this.data.attitudeRate ? this.data.attitudeRate : 5,
      speedScore: this.data.speedRate ? this.data.speedRate : 5,
      content: this.data.content,
      commCate: this.data.ids
    }
    let token = wx.getStorageSync('token')
    config._post('/order/comment', token, params).then(res => {
      console.log(res)
      console.log('asdascacsas',this.data.attitudeRate)
      console.log(this.data.speedRate)
      console.log(this.data.speedRate)
      if (res.code == 0) {
        $Toast({
          content: '评论成功'
        });
        that.data.orderList = []
        that.data.currentPage = 1
        that.getOrderList(2)
        this.setData({
          hidden: false,
        })
      }
    })
  },
  // 获取用户订单
  getOrderList(key) {
    wx.showLoading({
      title: '加载中'
    });
    let params = {
      userId: wx.getStorageSync('userId'),
      orderStatus: key,
      limit: 10,
      currentPage: this.data.currentPage
    }
    let token = wx.getStorageSync('token')
    config._post('/order/getByUserId', token, params).then(res => {
      wx.hideLoading()
      wx.stopPullDownRefresh()
      if (res.code == 0 && res.data.list && res.data.list.length > 0) {
        for (let i = 0; i < res.data.list.length; i++) {
          this.data.orderList.push(res.data.list[i])
        }
        this.setData({
          orderList: this.data.orderList,
        })
      } else if (this.data.currentPage > 1) {
        $Toast({
          content: '暂无更多订单！'
        })
      } else {
        this.setData({
          orderList: [],
          currentPage: 1
        });
      }
      wx.stopPullDownRefresh()
    })
  },

  loadMoreOrderList() {
    let params = {
      userId: wx.getStorageSync('userId'),
      orderStatus: this.data.current,
      limit: 10,
      currentPage: this.data.currentPage
    }
    let token = wx.getStorageSync('token')
    config._post('/order/getByUserId', token, params).then(res => {
      if (res.code == 0 && res.data.list && res.data.list.length > 0) {
        let orderList = this.data.orderList.concat(res.data.list)
        this.setData({
          orderList: orderList,
          currentPage: res.data.currPage + 1
        });
      }
      wx.stopPullDownRefresh()
    })
  },

  // 切换Tab
  handleChange(e) {
    var that = this
    this.setData({
      current: e.detail.key,
      currentPage: 1
    })
    that.data.currentPage = 1
    that.data.orderList = []
    this.getOrderList(e.detail.key)
  },
  // //去评价
  // handlerComment(e) {
  //   wx.navigateTo({
  //     url: '../comment/index?orderId=' + e.currentTarget.dataset.orderid,
  //   })
  // },
  // 确认送还衣服
  returnClothes(e) {
    var that = this
    let params = {
      orderId: e.currentTarget.dataset.orderid,
    }
    let token = wx.getStorageSync('token')
    config._post('/order/returnClothes', token, params).then(res => {
      if (res.code == 0) {
        $Toast({
          content: '衣服已确认送还'
        })
        that.data.currentPage = 1
        that.data.orderList = []
        this.getOrderList(2)
        that.setData({
          current: 2
        })
      }
    })
  },
  // 取消订单
  cancelOrder(e) {
    var that = this
    let formId = e.detail.formId
    let params = {
      orderId: e.currentTarget.dataset.orderid,
      formId: formId,
    }
    let token = wx.getStorageSync('token')
    wx.showModal({
      title: '提示',
      content: '确认取消订单吗？',
      success(res) {
        if (res.confirm) {
          config._post('/order/cancle', token, params).then(res => {
            if (res.code == 0) {
              $Toast({
                content: '订单已取消'
              })
              that.data.currentPage = 1
              that.data.orderList = []
              that.getOrderList(1)
            }
          })
        } else if (res.cancel == false) {}
      }
    })
  },

  // 删除订单
  deleteOrder(e) {
    var that = this;
    let params = {
      orderId: e.currentTarget.dataset.orderid,
    }
    let token = wx.getStorageSync('token')
    wx.showModal({
      title: '提示',
      content: '确认删除订单吗？',
      success(res) {
        if (res.confirm == true) {
          config._post('/order/delete', token, params).then(res => {
            if (res.code == 0) {
              $Toast({
                content: '订单已删除'
              })
              that.data.currentPage = 1
              that.data.orderList = []
              that.getOrderList(2)
              that.setData({
                current: 2
              })
            }
          })
        } else if (res.cancel == false) {}
      }
    })
  },
  //去支付
  topay(e) {
    wx.navigateTo({
      url: '../pay/index?orderId=' + e.currentTarget.dataset.orderid,
    })
    // let params = {
    //     orderId: e.currentTarget.dataset.orderid,
    // }
    // let token = wx.getStorageSync('token')
    // config._post('/pay/wxPay', token, params).then(res => {
    //     if (res.code == 0 && res.data) {
    //         wx.requestPayment({
    //             timeStamp: res.data.timeStamp,
    //             nonceStr: res.data.nonceStr,
    //             package: res.data.package,
    //             signType: 'MD5',
    //             paySign: res.data.paySign,
    //             success(res) {
    //                 $Toast({
    //                     content: '支付成功'
    //                 });
    //             },
    //             fail(res) {
    //                 $Toast({
    //                     content: '支付失败'
    //                 });
    //             }
    //         })
    //     } else {
    //         $Toast({
    //             content: '支付失败'
    //         });
    //     }
    // })
  },

  call(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.mobile //仅为示例，并非真实的电话号码
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function(options) {
    this.data.currentPage = 1
    this.setData({
      orderList: []
    })
    this.getOrderList(Number(this.data.current))
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.data.currentPage = 1
    this.data.orderList = []
    this.getOrderList(this.data.current)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.data.currentPage += 1
    this.getOrderList(this.data.current)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})